$(function () {
  const login_state = "LOGIN_STATE";
  let state = getCookie(login_state);
  if (!(state && state == "OK")) {
    history.back();
    return;
  }

  let address = [
    {
      province: "湖南省",
      city: "衡阳市",
      district: "蒸湘区",
      area: "华兴街道",
      address: "棕榈园3期28栋",
      tel: "157****1722",
      consignee: "郭烨",
      is_default: true,
    },
    {
      province: "湖南省",
      city: "郴州市",
      district: "汝城县",
      area: "盈洞瑶族乡",
      address: "深山村茶头托组",
      tel: "157****1722",
      consignee: "郭宗慈",
      is_default: false,
    },
    {
      province: "湖南省",
      city: "长沙市",
      district: "宁乡市",
      area: "大屯营镇",
      address: "二环南路格林春天6栋",
      tel: "185****5850",
      consignee: "郭振华",
      is_default: false,
    },
    {
      province: "湖南省",
      city: "长沙市",
      district: "岳麓区",
      area: "咸嘉湖街道",
      address: "社区卫生服务中心",
      tel: "185****5850",
      consignee: "郭啸天",
      is_default: false,
    },
  ];

  function renderAddress() {
    let shdz = address.filter((item) => {
      return item.is_default;
    })[0];
    let shdz_html = `<div class="diqu">${shdz.province}${shdz.city}${shdz.district}${shdz.area}</div>
                    <div class="dizhi">${shdz.address}</div>
                    <div class="name-tel">${shdz.consignee}  ${shdz.tel}</div>`;
    $(".changeAddress").html(shdz_html);
  }
  function renderAddressList() {
    let shdz_all_html = address
      .map((item, index) => {
        return ` <div class="address ${
          item.is_default ? "active" : ""
        }" index="${index}" >
            <div class="diqu">${item.province}${item.city}${item.district}${
          item.area
        }</div>
            <div class="dizhi">${item.address}</div>
            <div class="name-tel">${item.consignee}  ${item.tel}</div>
        </div>`;
      })
      .join("");
    $(".addressList").html(shdz_all_html);
  }
  renderAddress();
  renderAddressList();

  function renderOrder(data) {
    let html = data
      .map((item) => {
        return `<div class="order-item">
        <img src="${item.img}" alt="">
        <div class="item">
            <div class="item-name">${item.name}</div>
            <div class="item-pn"><span class="price">&yen;${item.price}</span><span class="num">x ${item.cart_num}</span></div>
        </div>
    </div>`;
      })
      .join("");
    $(".order-info").html(html);
  }

  let order = sessionStorage.getItem("order");
  if (!order) {
    location.href = "../html/cart.html";
    return;
  }
  order = JSON.parse(order);
  $(".totalMoney").text(order.total);
  renderOrder(order.data);

  $(".changeAddress").click(() => {
    $(".addressList").css({ transform: "translateY(0)" });
  });

  $(".addressList").on("click", ".address", (e) => {
    $(".addressList").css({ transform: "translateY(120%)" });
    let index = $(e.target).attr("index") * 1;
    console.log(index);
    address.forEach((item) => {
      item.is_default = false;
    });
    address[index].is_default = true;

    console.log(address);

    renderAddress();
    renderAddressList();
  });

  //   let obj = {
  //     cart_id: item.cart_id,
  //     user_id: item.user_id,
  //     goods_id: item.goods_id,
  //     cart_num: item.cart_num,
  //     total: item.cart_num * item.goods_new_price,
  //     img: item.img,
  //     name: item.goods_name,
  //     price: item.goods_new_price,
  //   };
  let flag = true;
  $(".tijiao").click(function () {
    let oD = order;
    oD.data.forEach((item) => {
      delete item["user_id"];
      delete item["img"];
      delete item["name"];
      delete item["price"];
    });
    if (!oD || !oD.data || oD.data.length == 0) return;
    if (!flag) return;
    flag = false;

    let shdz = address.filter((item) => {
      return item.is_default;
    })[0];
    let sjdz = `${shdz.province}${shdz.city}${shdz.district}${shdz.area}${shdz.address}`;

    $.pajax({
      url: "/api/order.php",
      type: "post",
      dataType: "json",
      data: {
        type: "add",
        total: oD.total,
        user_id: oD.user_id,
        data: oD.data,
        sjr: shdz.consignee,
        tel: shdz.tel,
        sjdz: sjdz,
      },
    }).then((res) => {
      if (res.code == 1) {
        new Model({
          title: "信息",
          content: res.msg,
          showCancel: false,
          confirmCallbak: function () {
            this.close();
          },
        }).init();
        setTimeout(() => {
          sessionStorage.clear();
          location.href = "../html/myorder.html";
        }, 1500);
      }
    });
  });
});
